package com.upf1sh.utils;
//import org.springframework.http.*;
//import org.springframework.web.client.*;
//
//
//public class ThingsboardApiClient {
//    private static final String BASE_URL = "http://106.12.118.231:1088/api/v1/";
//
//    public static void main(String[] args) {
//        String deviceId = "25066400-5b3a-11ed-bb54-b94cc16f5dd5";
//        String url = BASE_URL + "devices/" + deviceId + "/timeseries";
//
//        HttpHeaders headers = new HttpHeaders();
//        headers.setBearerAuth(getAccessToken());
//
//        HttpEntity<String> entity = new HttpEntity<>(headers);
//
//        RestTemplate restTemplate = new RestTemplate();
//        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
//
//        if (response.getStatusCode().is2xxSuccessful()) {
//            String responseBody = response.getBody();
//            // 处理响应数据
//            System.out.println(responseBody);
//        } else {
//            System.out.println("请求出错，错误代码：" + response.getStatusCodeValue());
//        }
//    }
//
//    private static String getAccessToken() {
//        // TODO: 在此处实现获取访问令牌的逻辑
//        // 返回有效的访问令牌
//        return "YaVnSdtihbZfKMnm9OjN";
//    }
//}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

public class ThingsboardApiClient {
    public static void main(String[] args) throws IOException {
        // Thingsboard API URL
        String apiUrl = "http://106.12.118.231:1088/api/plugins/telemetry/{entityType}/{entityId}/timeseries/values";

        // Your device's entityType and entityId
        String entityType = "devices";
        String entityId = "25066400-5b3a-11ed-bb54-b94cc16f5dd5";

        // Create the URL object
        URL url = new URL(apiUrl.replace("{entityType}", entityType).replace("{entityId}", entityId));

        // Open a connection to the URL
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置用户名密码
        String username = "710320534@qq.com";
        String password = "12345678@a";
        String authString = username + ":" + password;
        String encodedAuthString = Base64.getEncoder().encodeToString(authString.getBytes());
        connection.setRequestProperty("Authorization", "Basic " + encodedAuthString);
        // Set request method to GET
        connection.setRequestMethod("GET");

        // Get the response code
        int responseCode = connection.getResponseCode();

        // If the response code indicates success
        if (responseCode == HttpURLConnection.HTTP_OK) {
            // Read the response body
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }

            // Print the received telemetry data
            System.out.println("Telemetry Data: " + response.toString());
        } else {
            // Print the error response
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }

            System.out.println("Error response: " + response.toString());
        }

        // Close the connection
        connection.disconnect();
    }
}